package com.example.jdbcdemo.service.Impl;

import com.example.jdbcdemo.mapper.*;
import com.example.jdbcdemo.pojo.*;
import com.example.jdbcdemo.service.ProductServiceInterface;
import com.example.jdbcdemo.service.RecordServiceInterface;
import com.example.jdbcdemo.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RecordServiceImpl implements RecordServiceInterface {
    @Autowired
    GroupIdMapMapper groupIdMapMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ProductServiceInterface productService;
    @Autowired
    CollectMoneyOrderMapper collectMoneyOrderMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    StorageMapper storageMapper;

    /**
     * 根据日期查出当天所有的出入库记录
     * @param date 要查询的日期
     * @return 以序号对应一组order
     */
    @Override
    public  List<Map<String, Object>> queryGroupByDate(String date){
        List<GroupIdMap> groupIdMapList=groupIdMapMapper.queryGroupIdMapByDate(date);//根据日期查找
        if (groupIdMapList.isEmpty()){
            return null;
        }
        List<Map<String, Object>> resultList=new LinkedList<>();
        for (GroupIdMap groupIdMap:groupIdMapList) {
            int groupType = groupIdMap.getGroupType();//标识是哪种表单：1销售2进货3调拨进货4调拨出货
            int groupId = groupIdMap.getId();//获取标识组id
            List<Order> orderList = orderMapper.queryOrderByGroupId(groupId);//根据表示组id获取所有订单
            Map<String, Object> recordMap = new LinkedHashMap<>();
            recordMap.put("groupId",groupId);
            recordMap.put("date", date);//日期
            int status=groupIdMap.getStatus();
            String statuss;
            switch (status){
                case 0:
                    statuss="代提交";
                    break;
                case 1:
                    statuss="代审核";
                    break;
                case 2:
                    statuss="审核通过";
                    break;
                case 3:
                    statuss="审核不通过";
                    break;
                case 4:
                    statuss="退货单";
                    break;
                default:
                    statuss="";
                    break;
            }
            recordMap.put("status",statuss);//当前订单状态
            String orderType;
            switch (groupType) {
                case 1:
                    orderType = "销售";
                    break;
                case 2:
                    orderType = "进货";
                    break;
                case 3:
                    orderType = "调拨进货";
                    break;
                case 4:
                    orderType = "调拨出货";
                    break;
                default:
                    orderType="";
                    break;
            }
            recordMap.put("orderType",orderType);//种类
            List<Map<String,String>> tempList=new LinkedList<>();
            for (Order order:orderList){
                Map<String,String> tempMap=new LinkedHashMap<>();
                tempMap.put("prodectId",String.valueOf(order.getProductId()));//获取商品Id
                tempMap.put("productName", productService.getProductNameById(order.getProductId()));//获取商品名称
                tempMap.put("number",String.valueOf(order.getNumber()));//获取数量
                tempMap.put("price",String.valueOf(order.getPrice()));//获取商品价格
                tempMap.put("storageId",String.valueOf(order.getStorageId()));//获取仓库Id
                tempList.add(tempMap);
            }
            recordMap.put("productData",tempList);
            resultList.add(recordMap);
        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> queryGroupList() {
        List<GroupIdMap> groupIdMapList=groupIdMapMapper.queryGroupIdMapList();
        if (groupIdMapList.isEmpty()){
            return null;
        }
        List<Map<String,Object>> resultList=new LinkedList<>();
        for (GroupIdMap groupIdMap:groupIdMapList) {
            int groupType = groupIdMap.getGroupType();//标识是哪种表单：1销售2进货3调拨进货4调拨出货
            int groupId = groupIdMap.getId();//获取标识组id
            List<Order> orderList = orderMapper.queryOrderByGroupId(groupId);//根据表示组id获取所有订单
            Map<String, Object> recordMap = new LinkedHashMap<>();
            recordMap.put("groupId",groupId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            recordMap.put("date", sdf.format(groupIdMap.getDate()));//日期
            int status=groupIdMap.getStatus();
            String statuss;
            switch (status){
                case 0:
                    statuss="代提交";
                    break;
                case 1:
                    statuss="代审核";
                    break;
                case 2:
                    statuss="审核通过";
                    break;
                case 3:
                    statuss="审核不通过";
                    break;
                case 4:
                    statuss="退货单";
                    break;
                default:
                    statuss="";
                    break;
            }
            recordMap.put("status",statuss);//当前订单状态
            String orderType;
            switch (groupType) {
                case 1:
                    orderType = "销售";
                    break;
                case 2:
                    orderType = "进货";
                    break;
                case 3:
                    orderType = "调拨进货";
                    break;
                case 4:
                    orderType = "调拨出货";
                    break;
                default:
                    orderType="";
                    break;
            }
            recordMap.put("orderType",orderType);//种类
            List<Map<String,String>> tempList=new LinkedList<>();
            for (Order order:orderList){
                Map<String,String> tempMap=new LinkedHashMap<>();
                tempMap.put("prodectId",String.valueOf(order.getProductId()));//获取商品Id
                tempMap.put("productName", productService.getProductNameById(order.getProductId()));//获取商品名称
                tempMap.put("number",String.valueOf(order.getNumber()));//获取数量
                tempMap.put("price",String.valueOf(order.getPrice()));//获取商品价格
                tempMap.put("storageId",String.valueOf(order.getStorageId()));//获取仓库Id
                tempList.add(tempMap);
            }
            recordMap.put("productData",tempList);
            resultList.add(recordMap);
        }
        return resultList;
    }

    @Override
    public Map<String,Object> queryGroupListById(int id) {
        GroupIdMap groupIdMap=groupIdMapMapper.queryGroupIdMapById(id);
        Map<String,Object> resultMap=new LinkedHashMap<>();
        resultMap.put("groupId",id);
        int groupType = groupIdMap.getGroupType();//标识是哪种表单：1销售2进货3调拨进货4调拨出货
        List<Order> orderList = orderMapper.queryOrderByGroupId(id);//根据表示组id获取所有订单
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        resultMap.put("date", sdf.format(groupIdMap.getDate()));//日期
        int status=groupIdMap.getStatus();
        String statuss;
        switch (status){
            case 0:
                statuss="代提交";
                break;
            case 1:
                statuss="代审核";
                break;
            case 2:
                statuss="审核通过";
                break;
            case 3:
                statuss="审核不通过";
                break;
            case 4:
                statuss="退货单";
                break;
            default:
                statuss="";
                break;
        }
        resultMap.put("status",statuss);//当前订单状态
        String orderType;
        switch (groupType) {
            case 1:
                orderType = "销售";
                break;
            case 2:
                orderType = "进货";
                break;
            case 3:
                orderType = "调拨进货";
                break;
            case 4:
                orderType = "调拨出货";
                break;
            default:
                orderType="";
                break;
        }
        resultMap.put("orderType",orderType);//种类
        List<Map<String,String>> tempList=new LinkedList<>();
        for (Order order:orderList){
            Map<String,String> tempMap=new LinkedHashMap<>();
            tempMap.put("prodectId",String.valueOf(order.getProductId()));//获取商品Id
            tempMap.put("productName", productService.getProductNameById(order.getProductId()));//获取商品名称
            tempMap.put("number",String.valueOf(order.getNumber()));//获取数量
            tempMap.put("price",String.valueOf(order.getPrice()));//获取商品价格
            tempMap.put("storageId",String.valueOf(order.getStorageId()));//获取仓库Id
            tempMap.put("storageName",storageMapper.queryStorageById(order.getStorageId()).getAddress());
            tempList.add(tempMap);
        }
        resultMap.put("productData",tempList);
        return resultMap;
    }

    @Override
    public List<Map<String, Object>> queryInputGroupList() {
        List<GroupIdMap> groupIdMapList=groupIdMapMapper.queryGroupIdMapByType(2);//进货单的grouptype是2
        if (groupIdMapList.isEmpty()){
            return null;
        }
        List<Map<String,Object>> resultList=new LinkedList<>();
        for (GroupIdMap groupIdMap:groupIdMapList) {
            int groupId = groupIdMap.getId();//获取标识组id
            List<Order> orderList = orderMapper.queryOrderByGroupId(groupId);//根据表示组id获取所有订单
            Map<String, Object> recordMap = new LinkedHashMap<>();
            recordMap.put("groupId",groupId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            recordMap.put("date", sdf.format(groupIdMap.getDate()));//日期
            recordMap.put("status","进货成功");//当前订单状态
            List<Map<String,String>> tempList=new LinkedList<>();
            for (Order order:orderList){
                Map<String,String> tempMap=new LinkedHashMap<>();
                tempMap.put("prodectId",String.valueOf(order.getProductId()));//获取商品Id
                tempMap.put("productName", productService.getProductNameById(order.getProductId()));//获取商品名称
                tempMap.put("number",String.valueOf(order.getNumber()));//获取数量
                tempMap.put("price",String.valueOf(order.getPrice()));//获取商品价格
                tempMap.put("storageId",String.valueOf(order.getStorageId()));//获取仓库Id
                tempMap.put("storageName",storageMapper.queryStorageById(order.getStorageId()).getAddress());
                tempList.add(tempMap);
            }
            recordMap.put("productData",tempList);
            resultList.add(recordMap);
        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> querySaleGroupList() {
        List<GroupIdMap> groupIdMapList=groupIdMapMapper.queryGroupIdMapByType(1);//销售单的grouptype是1
        if (groupIdMapList.isEmpty()){
            return null;
        }
        List<Map<String,Object>> resultList=new LinkedList<>();
        for (GroupIdMap groupIdMap:groupIdMapList) {
            int groupId = groupIdMap.getId();//获取标识组id
            List<Order> orderList = orderMapper.queryOrderByGroupId(groupId);//根据表示组id获取所有订单
            Map<String, Object> recordMap = new LinkedHashMap<>();
            recordMap.put("groupId",groupId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            recordMap.put("date", sdf.format(groupIdMap.getDate()));//日期
            int status=groupIdMap.getStatus();
            String statuss;
            switch (status){
                case 0:
                    statuss="代提交";
                    break;
                case 1:
                    statuss="待审核";
                    break;
                case 2:
                    statuss="审核通过";
                    break;
                case 3:
                    statuss="审核不通过";
                    break;
                case 4:
                    statuss="退货单";
                    break;
                default:
                    statuss="";
                    break;
            }
            if (status==2){
                CollectMoneyOrder collectMoneyOrder=collectMoneyOrderMapper.queryCollectMoneyOrderByGroupId(groupId);
                double profit=collectMoneyOrder.getProfit();//获取本次订单的利润
                recordMap.put("profit",profit);
            }
            recordMap.put("status",statuss);//当前订单状态
            recordMap.put("statusCode",status);//当前订单状态码
            List<Map<String,String>> tempList=new LinkedList<>();
            int customerType=3;
            Customer customer=new Customer();
            for (Order order:orderList){
                customer=customerMapper.queryCustomerById(order.getCustomerId());
                if (customer==null)
                {
                    resultList=new LinkedList<>();
                    return resultList;
                }
                customerType=customer.getCustomerType();
                Map<String,String> tempMap=new LinkedHashMap<>();
                tempMap.put("prodectId",String.valueOf(order.getProductId()));//获取商品Id
                tempMap.put("productName", productService.getProductNameById(order.getProductId()));//获取商品名称
                tempMap.put("number",String.valueOf(order.getNumber()));//获取数量
                tempMap.put("price",String.valueOf(order.getPrice()));//获取商品价格
                tempMap.put("storageId",String.valueOf(order.getStorageId()));//获取仓库Id
                tempMap.put("storageName",storageMapper.queryStorageById(order.getStorageId()).getAddress());
                tempList.add(tempMap);
            }
            String customertypess="";
            switch (customerType){
                case 1:
                    customertypess="批发";
                    break;
                case 0:
                    customertypess="零售";
                    break;
                default:
                    break;
            }
            recordMap.put("orderType",customertypess);
            recordMap.put("customerId",customer.getId());
            recordMap.put("customerName",customer.getName());
            recordMap.put("customerTel",customer.getTelphone());
            recordMap.put("customerAddress",customer.getAddress());

            recordMap.put("productData",tempList);
            resultList.add(recordMap);
        }
        return resultList;
    }

    //退货
    @Override
    public JsonUtil RefundByGroupId(int groupId) {
        GroupIdMap groupIdMap=groupIdMapMapper.queryGroupIdMapById(groupId);
        if (groupIdMap.getGroupType()==1){
            collectMoneyOrderMapper.deleteCollectMoneyOrderByGroupId(groupId);//根据groupid删除付款单
            groupIdMap.setStatus(4);//修改为退货单
            groupIdMapMapper.updateGroupIdMap(groupIdMap);
            List<Order> orderList= orderMapper.queryOrderByGroupId(groupId);
            for (Order order:orderList){
                Goods goods=goodsMapper.queryGoodsByTwo(order.getProductId(), order.getStorageId());
                int tempNum=goods.getNumber();
                goods.setNumber(tempNum+order.getNumber());
                goodsMapper.updateGoods(goods);
            }
            return new JsonUtil(0,"退货成功","");
        }
        return new JsonUtil(401,"非销售单不可退货","");
    }


}
